#include "AI.h"

	const int  AIMonster::AttackAnim =3 ;
	 const int  AIMonster::DeadAnim = 4;
	 const int  AIMonster::StandAnim = 0 ;
	 const int  AIMonster::MoveAnim = 1;

void AIMonster::Update(float pTime)//,Model* mModel, vector<Model*>* mTargets)
{
	//if(mModel!= NULL){
		Model* mTarget = NULL;
		float distance;
		float nDistance;
		D3DXVECTOR3* dir = new D3DXVECTOR3();
		D3DXVECTOR3* ndir = new D3DXVECTOR3();

		

		if(mCurtarget ==NULL){
 		if(mTargets->size() > 0){
			for(int a =0; a < mTargets->size(); a ++){
				if(!mTargets->at(a)->dead){
				if(mTarget ==NULL){
					mTarget = mTargets->at(a);
				
						dir->x = -1*mModel->GetPos()->x + mTarget->GetPos()->x;
						dir->y = -1*mModel->GetPos()->y + mTarget->GetPos()->y;
						dir->z = -1*mModel->GetPos()->z +mTarget->GetPos()->z;
						distance = ::D3DXVec3Length(dir);
						::D3DXVec3Normalize(dir,dir);
					
				}
				else{
					
					ndir->x = -1*mModel->GetPos()->x + mTargets->at(a)->GetPos()->x;
					ndir->y = -1*mModel->GetPos()->y + mTargets->at(a)->GetPos()->y;
					ndir->z = -1*mModel->GetPos()->z + mTargets->at(a)->GetPos()->z;
					nDistance = ::D3DXVec3Length(ndir);
					::D3DXVec3Normalize(ndir,ndir);
					
					if(nDistance < distance){
						distance = nDistance;
						dir = ndir;
						mTarget = mTargets->at(a);
					}
				}
				}
			}

			//mModel->SetDir(dir);
			mCurtarget = mTarget;

			if(mCurtarget!=NULL){
			//Find the vector to the 
			::D3DXVec3Subtract(dir,mCurtarget->GetPos(),mModel->GetPos());
			::D3DXVec3Normalize(dir,dir);

			D3DXVECTOR3 facing;
			facing.x = 0.0f;
			facing.y = 0.0f;
			facing.z = 1.0f;

			float angle = mModel->GetRot()->x;

			degree =  atan2(facing.z,facing.x) - atan2(dir->z,dir->x);

			if(degree <0){
				degree += 6.28f;
			}

			if(angle <0){
				angle += 6.28f;
			}

			float toTurn = abs(angle) - degree;

			if(abs(toTurn) != 0.0f){

				float amount;
				amount = abs((rotSpeed * pTime)/toTurn) *toTurn ;
				if(abs(amount) > abs(degree)){
					amount = toTurn;
				}

				turning = true;
				turnAngle = degree;
				turnVal = amount;
			}else
			{

				turning = false;
			}
			}
		}
		}

		if(mCurtarget == NULL){
		
			mModel->GetXFile()->SetAnimationSet(DeadAnim);
			mModel->GetXFile()->SetRepeat(false);
		}
		else
		{
			::D3DXVec3Subtract(dir,mCurtarget->GetPos(),mModel->GetPos());
			if(turning){
				float turnDiff = abs(mModel->GetRot()->x) - turnAngle;
				if(turnDiff ==0){
					turning = false;
					mModel->SetDir(dir);
				}else if(abs(turnDiff) < 2*abs(turnVal)){
					mModel->GetRot()->x += turnDiff;
				}
				else{
					mModel->GetRot()->x += (turnVal);
				}
				if(mModel->GetRot()->x >= 6.28f){
					mModel->GetRot()->x -= 6.28f;
				}else if(mModel->GetRot()->x <= -6.28f){
					mModel->GetRot()->x += 6.28f;
				}
			}
							
			else{

			mModel->SetDir(dir);
		
		
			float distance = ::D3DXVec3Length(dir);

		if(distance < attackDist){
			mModel->GetDir()->x = 0;
			mModel->GetDir()->y = 0;
			mModel->GetDir()->z = 0;
			mAttack = true;
		}else{
			mModel->GetPos()->x += mModel->GetDir()->x * mModel->GetSpeed() *pTime;
			mModel->GetPos()->y += mModel->GetDir()->y * mModel->GetSpeed() *pTime;
			mModel->GetPos()->z += mModel->GetDir()->z * mModel->GetSpeed() *pTime;
		}
		}
		
		int nextAnim = StandAnim;
		if( ::D3DXVec3Length(mModel->GetDir()) != 0){
			nextAnim = MoveAnim;
		}
		else{
			if(mAttack != true){
				nextAnim = StandAnim;
			}
			else{
				nextAnim = AttackAnim;
				 mModel->GetXFile()->SetAnimationSpeed(AttackAnim,attackDelay);
			}		
		}

		if(curAnim!= nextAnim){
			mModel->GetXFile()->SetAnimationSet(nextAnim);
			curAnim = nextAnim;
		}

		switch(curAnim){

			case AttackAnim:
				
				curDelay += pTime;

				if(curDelay >= attackDelay){
					curDelay -= attackDelay;
					mCurtarget->SetHealth(mCurtarget->health -1);
				}

				if(mCurtarget->dead == true){
					mCurtarget = NULL;
					mAttack = false;
				}

				break;

		}
		}
		
}

void AIMonster::SetModel(Model* m){
		 mModel = m;

		 //mModel->GetXFile()->SetAnimationSet(StandAnim);
		
	 }

float AIMonster::get_angle_between_in_radians(D3DXVECTOR3* a, D3DXVECTOR3* b)
{

 float result;

 result = (atan2(a->z, a->x) - atan2(b->z, b->x)) * 180.0 * 3.14;

 if(result < 0)
  result += 360.0;

 return result;

	/*
   float dotproduct, lengtha, lengthb, result;

   dotproduct = (ax * bx) + (ay * by);
   lengtha = sqrt(ax * ax + ay * ay);
   lengthb = sqrt(bx * bx + by * by);
   if(lengtha == 0 || lengthb == 0){
		return 0;
   }
   result = acos( dotproduct / (lengtha * lengthb) );

   if(dotproduct < 0)
   {
      if(result > 0)
         result += 3.14f;
      else
         result -= 3.14f;
   }
   return result;
   */
}